﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using System.Linq;
using System.IO;
using HtmlAgilityPack;
using System.Diagnostics;
using SVM;
using YahContent;

namespace GetConvert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Text += @" " + DateTime.Now.ToString(CultureInfo.InvariantCulture);
            comboLinkNews.Sorted = true;
            button5_Click(sender, e);
            buttonGetLink_Click(sender, e);
            try
            {
                _problem = Problem.Read(Filetrain);
                _numberCount = _problem.Count;
                _numberVectorA = 0;
                _numberVectorB = 0;
                foreach (var d in _problem.Y)
                {
                    if (d > 0)
                    {
                        _numberVectorA++;
                    }
                    else
                    {
                        _numberVectorB++;
                    }
                }
                _model = Model.Read(Filemodel);
                richTextBox1.Text = _numberCount.ToString(CultureInfo.InvariantCulture) + '\n' +
                                    _numberVectorA.ToString(CultureInfo.InvariantCulture) + '\n' +
                                    _numberVectorB.ToString(CultureInfo.InvariantCulture);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private IEnumerable<string> GetLink(string textUrl)
        {
            textUrl = textUrl.Trim();
            var lislink = new List<string>();
            try
            {
                var getWeb = new HtmlWeb();
                var document = getWeb.Load(textUrl);
                var linksOnPage = from lnks in document.DocumentNode.Descendants()
                                  where lnks.Name == "a" &&
                                        lnks.Attributes["href"] != null &&
                                        lnks.Attributes["href"].Value.Trim().Length > textUrl.Length*5/2
                                  select new
                                      {
                                          URL = lnks.Attributes["href"].Value.Trim()
                                      };

                foreach (var i in linksOnPage)
                {
                    var url = i.URL;
                    if (url.IndexOf("/", StringComparison.Ordinal) == 0)
                    {
                        url = textUrl + i.URL;
                    }
                    if (url.IndexOf("http://", StringComparison.Ordinal) != 0)
                    {
                        url = textUrl + "/" + i.URL;
                    }
                    lislink.Remove(url);
                    lislink.Add(url);
                }
                lislink.Sort();
                for (int i = 1; i < lislink.Count; ++i)
                {
                    if (lislink[i].IndexOf(lislink[i - 1], System.StringComparison.Ordinal) != -1)
                    {
                        lislink.Remove(lislink[i - 1]);
                    }
                }
            }
            catch
            {
                MessageBox.Show(@"lỗi Gét link", @"Thông báo");
            }

            return lislink;
        }

        private void buttonGetLink_Click(object sender, EventArgs e)
        {
            comboUrlHomes.SelectedIndex++;
            var lislink = GetLink(comboUrlHomes.Text);
            comboLinkNews.Items.Clear();
            foreach (var i in lislink)
            {
                comboLinkNews.Items.Add(i);
            }
            button4.Text = comboLinkNews.Items.Count.ToString(CultureInfo.InvariantCulture);
        }

        private HtmlNode _body;

        private void button3_Click(object sender, EventArgs e)
        {
            comboVextor.Items.Clear();
            comboVextor.Text = "";
            comboXpath.Items.Clear();
            comboBoNodeHtml.Items.Clear();
            combolabel.Items.Clear();
            webBrowser1.DocumentText = "";
            try
            {
                try
                {
                    comboLinkNews.Items.Remove(comboLinkNews.Text);
                    comboLinkNews.Text = comboLinkNews.Items[0].ToString();
                }
                catch (Exception)
                {
                    buttonGetLink_Click(sender, e);
                }
                var getContent = new DataExtractor(comboLinkNews.Text);
                var nodeBody = getContent.GetNodeBody();
                var listNode = nodeBody.Descendants();
                var timer = new Stopwatch();
                timer.Start();
                foreach (var node in listNode)
                {
                    if (node.InnerText.Length > 128)
                    {
                        var dataAttri = getContent.Callpoint(node);
                        if (dataAttri.NumberOfLongText > 0)
                        {
                            comboVextor.Items.Add(dataAttri.GetVector());
                            comboBoNodeHtml.Items.Add(node.InnerHtml);
                            comboXpath.Items.Add(node.XPath);
                            combolabel.Items.Add(Prediction.Predict(_model, dataAttri.GetNodes()));
                        }
                    }
                }
                timer.Stop();
                textBox2.Text = timer.Elapsed.TotalSeconds.ToString(CultureInfo.InvariantCulture) + @"s";
                _body = nodeBody;
                webBrowser1.DocumentText = @"<html><body>" + _body.InnerHtml + @"</body></html>";
                button4.Text = comboLinkNews.Items.Count.ToString(CultureInfo.InvariantCulture);
                butGetVector.Text = @"So Node:" + comboVextor.Items.Count.ToString(CultureInfo.InvariantCulture);
                button4.Text = comboLinkNews.Items.Count.ToString(CultureInfo.InvariantCulture);
                if (comboVextor.Items.Count == 0)
                {
                    button3_Click(sender, e);
                }
            }
            catch (Exception)
            {
                button3_Click(sender, e);
            }
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboXpath.SelectedIndex = comboVextor.SelectedIndex;
            comboBoNodeHtml.SelectedIndex = comboVextor.SelectedIndex;
            combolabel.SelectedIndex = comboVextor.SelectedIndex;
            webBrowser1.DocumentText = comboBoNodeHtml.Text;
        }

        private void WriteFileTrain()
        {
            var sw = new StreamWriter(Filelink, true);
            sw.WriteLine(comboLinkNews.Text);
            sw.Close();
            sw = new StreamWriter(Filetrain, true);
            if (comboVextor.Text != "")
            {
                sw.WriteLine("+1 " + comboVextor.Text);
                var sw2 = new StreamWriter(FileNodeHtml,true);
                sw2.WriteLine("<table border=\"2\"><td>"+
                    comboBoNodeHtml.Text.Replace("\r", " ")
                                   .Replace("\n", " ")
                                   .Replace("\t", " ")
                                   .Replace("\v", " ")
                                   .Replace("\f", " ")
                                   .Trim()+"</td></table>");
                sw2.Close();
                _numberVectorA++;
                _numberCount++;
                comboVextor.Items.Remove(comboVextor.Text);
            }
            foreach (string row in comboVextor.Items)
            {
                sw.WriteLine("-1 " + row);
                _numberVectorB++;
                _numberCount++;
            }
            sw.Close();
            richTextBox1.Text = _numberCount.ToString(CultureInfo.InvariantCulture) + '\n' +
                                _numberVectorA.ToString(CultureInfo.InvariantCulture) + '\n' +
                                _numberVectorB.ToString(CultureInfo.InvariantCulture);
        }

        private void button1_Click_3(object sender, EventArgs e)
        {
            if (comboVextor.Text == "")
            {
                if (
                    MessageBox.Show(@"Không có nội dung bài viêt, tât cả các thẻ thuộc -1 bạn co muốn ghi không?",
                                    @"Thông Báo Ghi Fiel Train", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    WriteFileTrain();
                }
            }
            else
            {
                WriteFileTrain();
            }
            comboVextor.Items.Clear();
            comboXpath.Items.Clear();
            comboBoNodeHtml.Items.Clear();
            combolabel.Items.Clear();
            webBrowser1.DocumentText = "";
            button3_Click(sender, e);
        }

        private void button5_Click(object sender, EventArgs e)
        {
            var sr = new StreamReader(FileUrlHome);
            string u = sr.ReadLine();
            while (u != null)
            {
                comboUrlHomes.Items.Add(u);
                u = sr.ReadLine();
            }

            comboUrlHomes.SelectedIndex = 0;
            sr.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (_body != null)
            {
                webBrowser1.DocumentText = @"<html><body>" + _body.InnerHtml + @"</body></html>";
            }
        }

        private void button3_Click_1(object sender, EventArgs e)
        {
            var timer = new Stopwatch();
            timer.Start();
            _problem = Problem.Read(Filetrain);
            _numberCount = _problem.Count;
            _numberVectorA = 0;
            _numberVectorB = 0;
            foreach (var d in _problem.Y)
            {
                if (d > 0)
                {
                    _numberVectorA++;
                }
                else
                {
                    _numberVectorB++;
                }
            }
            richTextBox1.Text = _numberCount.ToString(CultureInfo.InvariantCulture) + '\n' +
                                _numberVectorA.ToString(CultureInfo.InvariantCulture) + '\n' +
                                _numberVectorB.ToString(CultureInfo.InvariantCulture);
            var parameters = new Parameter();
            try
            {
                ParameterSelection.Grid(_problem, parameters, Fileparam, out _c, out _gamma);
                parameters.C = _c;
                parameters.Gamma = _gamma;
                _model = Training.Train(_problem, parameters);
                Model.Write(Filemodel, _model);
            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.ToString());
            }
           
            
            timer.Stop();
            button3.Text = @"Traing: " + timer.Elapsed.TotalSeconds.ToString(CultureInfo.InvariantCulture) + @"s";
        }

        private void combolabel_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboVextor.SelectedIndex = combolabel.SelectedIndex;
        }

        private void button4_Click(object sender, EventArgs e)
        {
            comboLinkNews.Items.Clear();
            var sr = new StreamReader(Filelink);
            do
            {
                var item = sr.ReadLine();
                if (item == null) continue;
                comboLinkNews.Items.Remove(item);
                comboLinkNews.Items.Add(item);
            } while (!sr.EndOfStream);
            sr.Close();
            var sw = new StreamWriter(Filelink);
            foreach (var item in comboLinkNews.Items)
            {
                sw.WriteLine(item);
            }
            sw.Close();
            button4.Text = comboLinkNews.Items.Count.ToString(CultureInfo.InvariantCulture);
        }

        private void comboXpath_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboVextor.SelectedIndex = comboXpath.SelectedIndex;
        }
    }
}